//
//  HSYBaseViewController.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseViewController.h"
#import "UINavigationController+Push.h"
#import "UINavigationController+PoP.h"
#import "HSYBaseNavigationController.h"
#import "HSYBaseCardNavigationController+Animation.h"
#import "HSYBaseMoveNavigationController+Animation.h"

#define NAVIGATIONCONTROLLER_POP_ANIMATION_TYPE         kHSYCustomViewAnimationTransitionTypeReveal

@implementation HSYBaseViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setNavigationBarLeftButtonWithImage:BACK_IMAGE_NOR hightImage:BACK_IMAGE_PRE action:@selector(popToVC) open:YES];
}

- (void)setSkinNavBarColor:(UIColor *)skinNavBarColor {
    
    _skinNavBarColor = skinNavBarColor;
    [[UINavigationBar appearance] setBackgroundColor:skinNavBarColor];//设置nav的bar颜色为皮肤色
    
}


#pragma mark - BaseVC POP

- (void)popToVC {
    
    [self popToVCAnimated:YES];
}

- (void)popToVCAnimated:(BOOL)animated {
    
    [self popToViewControllerWithAnimated:animated];
}

- (void)popToRootVCAnimated:(BOOL)animated {
    
    [self.navigationController customPoPToRootViewControllerWithTransition:NAVIGATIONCONTROLLER_POP_ANIMATION_TYPE subtype:HSYCustomViewAnimationSubtypeFromLeft completion:^{
        
    }];
}

- (NSArray *)popToViewControllerWithLevel:(NSInteger)level animated:(BOOL)animated
{
    NSInteger viewControllersCount = self.navigationController.viewControllers.count;
    if (viewControllersCount > level) {
        
        NSInteger idx = viewControllersCount - level - 1;
        UIViewController *viewController = self.navigationController.viewControllers[idx];
        static NSArray *popViewControllers = nil;
        
        [[[self.navigationController customPoPToViewController:viewController withCustomTransition:NAVIGATIONCONTROLLER_POP_ANIMATION_TYPE subtype:HSYCustomViewAnimationSubtypeFromLeft] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSArray *x) {
            popViewControllers = x;
        }];
        return popViewControllers;
        
    } else {
        return [self.navigationController customPoPToRootViewControllerWithTransition:NAVIGATIONCONTROLLER_POP_ANIMATION_TYPE subtype:HSYCustomViewAnimationSubtypeFromLeft completion:^{
            
        }];
    }
}

- (void)popToViewControllerWithAnimated:(BOOL)animated {
    
    if (self.navigationController.viewControllers.count <2) {
        return;
    }
    HSYBaseNavigationController *nav = (HSYBaseNavigationController *)self.navigationController;
    switch (nav.animationType) {
        case kHSYTransitionsAnimationTypeMove: {
            [(HSYBaseMoveNavigationController *)self.navigationController customMovePOPViewControllerForAnimated:YES completion:^(BOOL isAnimation) {
                
            }];
        }
            break;
            
        case kHSYTransitionsAnimationTypeCard: {
            [(HSYBaseCardNavigationController *)self.navigationController customCardPOPViewControllerForAnimated:animated withCompletion:^(BOOL isAnimation) {
                
            }];
        }
            break;
            
        case kHSYTransitionsAnimationTypeRotation: {
            
        }
            break;
            
        case kHSYTransitionsAnimationTypeDone: {
            [self.navigationController customPoPViewControllerWithTransition:NAVIGATIONCONTROLLER_POP_ANIMATION_TYPE subtype:HSYCustomViewAnimationSubtypeFromLeft completion:^{
                
            }];
        }
            break;
            
        default:
            break;
    }

}


#pragma mark - BaseVC Push

- (void)pushToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (!viewController) {
        return;
    }
    HSYBaseNavigationController *nav = (HSYBaseNavigationController *)self.navigationController;
    switch (nav.animationType) {
        case kHSYTransitionsAnimationTypeMove: {
            [(HSYBaseMoveNavigationController *)self.navigationController customMovePushViewController:viewController animated:YES completion:^(BOOL isAnimation) {
                
            }];
        }
            break;
        
        case kHSYTransitionsAnimationTypeCard: {
            [(HSYBaseCardNavigationController *)self.navigationController customCardPushViewController:viewController animated:animated completion:^(BOOL isAnimation) {
                
            }];
        }
            break;
            
        case kHSYTransitionsAnimationTypeRotation: {
            
        }
            break;
            
        case kHSYTransitionsAnimationTypeDone: {
            [self.navigationController customPushViewController:viewController withCustomTransitions:NAVIGATIONCONTROLLER_POP_ANIMATION_TYPE subtype:HSYCustomViewAnimationSubtypeFromRight completion:^{
            }];
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - BaseVC Dismiss ViewController

- (void)dismissToViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
    
    [self dismissViewControllerAnimated:animated completion:completion];
}

#pragma mark - Search NAV ViewControllers

- (UIViewController *)findNavigationControllerSubViewControllerToClassName:(NSString *)className
{
    if (!className || className.length == 0) {
        return nil;
    }
    for (UIViewController *viewController in self.navigationController.viewControllers) {
        
        if ([viewController isKindOfClass:NSClassFromString(className)]) {
            return viewController;
        }
    }
    return nil;
}



#pragma mark - RAC For HUD

- (NSInteger)requestStateCodeWithStateCode:(id)stateCode {
    
    if([stateCode isKindOfClass:[NSError class]]) {
        
        [HSYHUDHelper hideAllHUDView];
        NSError *error = (NSError *)stateCode;
        if (error.userInfo[kErrorKey]) {

            [HSYHUDModel initWithShowHUDType:kShowHUDViewTypeWrong codeType:kHSYHUDModelCodeTypeDefault text:error.userInfo[kErrorKey] animationTime:HUD_HIDE_TIME isCanTouch:NO];
            
        } else if(error.userInfo[NSLocalizedDescriptionKey]) {
            
            [HSYHUDModel initWithShowHUDType:kShowHUDViewTypeWrong codeType:kHSYHUDModelCodeTypeDefault text:error.userInfo[NSLocalizedDescriptionKey] animationTime:HUD_HIDE_TIME isCanTouch:NO];
        }
        return error.code;
        
    } else if ([stateCode isKindOfClass:[HSYHUDModel class]]) {
        
        HSYHUDModel *model = (HSYHUDModel *)stateCode;
        if (model.codeType == kHSYHUDModelCodeTypeUpdateLoading) {
            
            [HSYHUDHelper hideAllHUDView];
            [HSYHUDHelper showHUDViewForShowType:model.showType text:model.hudString hideAfter:model.animationTime isTouchCancel:model.isTouchCancel];
            
        } else if(model.codeType == kHSYHUDModelCodeTypeRequestSuccess) {
            
            [HSYHUDHelper hideAllHUDView];//清除loading页面
            
        } else if (model.codeType == kHSYHUDModelCodeTypeRequestFailure) {
            
            [HSYHUDHelper hideAllHUDView];//清除loading页面
            [HSYHUDHelper showHUDViewForShowType:model.showType text:model.hudString hideAfter:model.animationTime isTouchCancel:model.isTouchCancel];
        }
        
        return model.codeType;
    }
    
    return 0;
}


#pragma mark - Bar Height


- (CGFloat)getTopBarHeight {
    
    return [self getStatusBarHeight] + [self getNavigationBarHeight];
}

- (CGFloat)getStatusBarHeight {
    
    return [UIScreen getStatusBarHeight];
}

- (CGFloat)getNavigationBarHeight {
    
    return self.navigationController.navigationBar.viewHeight;
}

@end
